{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "cell_0",
      "metadata": {},
      "source": [
        "# ContextGem: LabelConcept with References and Justifications"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_1",
      "metadata": {},
      "outputs": [],
      "source": [
        "%pip install -U contextgem"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "cell_2",
      "metadata": {},
      "source": [
        "To run the extraction, please provide your LLM details in the ``DocumentLLM(...)`` constructor further below."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_3",
      "metadata": {},
      "outputs": [],
      "source": [
        "# ContextGem: LabelConcept with References and Justifications\n",
        "\n",
        "import os\n",
        "\n",
        "from contextgem import Document, DocumentLLM, LabelConcept\n",
        "\n",
        "\n",
        "# Create a Document with content that might be challenging to classify\n",
        "mixed_content_text = \"\"\"\n",
        "QUARTERLY BUSINESS REVIEW AND POLICY UPDATES\n",
        "GlobalTech Solutions Inc. - February 2025\n",
        "\n",
        "EMPLOYMENT AGREEMENT AND CONFIDENTIALITY PROVISIONS\n",
        "\n",
        "This Employment Agreement (\"Agreement\") is entered into between GlobalTech Solutions Inc. (\"Company\") and Sarah Johnson (\"Employee\") as of February 1, 2025.\n",
        "\n",
        "EMPLOYMENT TERMS\n",
        "Employee shall serve as Senior Software Engineer with responsibilities including software development, code review, and technical leadership. The position is full-time with an annual salary of $125,000.\n",
        "\n",
        "CONFIDENTIALITY OBLIGATIONS\n",
        "Employee acknowledges that during employment, they may have access to confidential information including proprietary algorithms, customer data, and business strategies. Employee agrees to maintain strict confidentiality of such information both during and after employment.\n",
        "\n",
        "NON-COMPETE PROVISIONS\n",
        "For a period of 12 months following termination, Employee agrees not to engage in any business activities that directly compete with Company's core services within the same geographic market.\n",
        "\n",
        "INTELLECTUAL PROPERTY\n",
        "All work products, inventions, and discoveries made during employment shall be the exclusive property of the Company.\n",
        "\n",
        "ADDITIONAL INFORMATION:\n",
        "\n",
        "FINANCIAL PERFORMANCE SUMMARY\n",
        "Q4 2024 revenue exceeded projections by 12%, reaching $3.2M. Cost optimization initiatives reduced operational expenses by 8%. The board approved a $500K investment in new data analytics infrastructure for 2025.\n",
        "\n",
        "PRODUCT LAUNCH TIMELINE\n",
        "The AI-powered customer analytics platform will launch Q2 2025. Marketing budget allocated: $200K for digital campaigns. Expected customer acquisition target: 150 new enterprise clients in the first quarter post-launch.\n",
        "\"\"\"\n",
        "\n",
        "doc = Document(raw_text=mixed_content_text)\n",
        "\n",
        "# Define a LabelConcept with justifications and references enabled\n",
        "document_classification_concept = LabelConcept(\n",
        "    name=\"Document Classification with Evidence\",\n",
        "    description=\"Classify this document type and provide reasoning for the classification\",\n",
        "    labels=[\n",
        "        \"Employment Contract\",\n",
        "        \"NDA\",\n",
        "        \"Consulting Agreement\",\n",
        "        \"Service Agreement\",\n",
        "        \"Partnership Agreement\",\n",
        "        \"Other\",\n",
        "    ],\n",
        "    classification_type=\"multi_class\",  # a single label is always returned\n",
        "    add_justifications=True,  # enable justifications to understand classification reasoning\n",
        "    justification_depth=\"comprehensive\",  # provide detailed reasoning\n",
        "    justification_max_sents=5,  # allow up to 5 sentences for justification\n",
        "    add_references=True,  # include references to source text\n",
        "    reference_depth=\"paragraphs\",  # reference specific paragraphs that informed classification\n",
        "    singular_occurrence=True,  # expect only one classification result\n",
        ")\n",
        "\n",
        "# Attach the concept to the document\n",
        "doc.add_concepts([document_classification_concept])\n",
        "\n",
        "# Configure DocumentLLM with your API parameters\n",
        "llm = DocumentLLM(\n",
        "    model=\"azure/gpt-4.1\",\n",
        "    api_key=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_KEY\"),\n",
        "    api_version=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_VERSION\"),\n",
        "    api_base=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_BASE\"),\n",
        ")\n",
        "\n",
        "# Extract the concept from the document\n",
        "document_classification_concept = llm.extract_concepts_from_document(doc)[0]\n",
        "\n",
        "# Display the classification results with evidence\n",
        "if document_classification_concept.extracted_items:\n",
        "    item = document_classification_concept.extracted_items[0]\n",
        "\n",
        "    print(\"=== DOCUMENT CLASSIFICATION RESULTS ===\")\n",
        "    print(f\"Classification: {item.value[0]}\")\n",
        "    print(\"\\nJustification:\")\n",
        "    print(f\"{item.justification}\")\n",
        "\n",
        "    print(\"\\nEvidence from document:\")\n",
        "    for i, paragraph in enumerate(item.reference_paragraphs, 1):\n",
        "        print(f\"{i}. {paragraph.raw_text}\")\n",
        "\n",
        "else:\n",
        "    print(\"No classification could be determined - none of the predefined labels apply\")\n",
        "\n",
        "# This example demonstrates how justifications help explain why the LLM\n",
        "# chose a specific classification and how references show which parts\n",
        "# of the document informed that decision\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}